import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:whale/helper/utils/util.dart';

import '../../../../helper/component/base/refresh/tab_refresh/view.dart';
import 'index.dart';

class TabDemoPage extends ITabRefreshPage<TabDemoController> {
  const TabDemoPage({Key? key}) : super(key: key);

  @override
  Widget buildRefreshBody(BuildContext context) {
    return buildUpdateTabBarView(context);
  }

  @override
  Widget buildView(BuildContext context, Widget refreshBody) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("tab_demo"),
        actions: [
          TextButton(onPressed: controller.refreshTitle, child: Text('刷新')),
        ],
      ),
      body: SafeArea(
        child: Column(
          children: [
            buildUpdateTabBar(context, true),
            Expanded(child: refreshBody),
          ],
        ),
      ),
    );
  }

  @override
  Widget indexPageBuilder(BuildContext context, int index,
      {ScrollPhysics? physics}) {
    return _TabItemPage(text: '$index');
  }
}

class _TabItemPage extends StatefulWidget {
  const _TabItemPage({super.key, required this.text});
  final String text;
  @override
  State<_TabItemPage> createState() => __TabItemPageState();
}

class __TabItemPageState extends State<_TabItemPage> {
  bool isLoadFish = false;
  @override
  void initState() {
    super.initState();
    Future.delayed(Duration(seconds: 1)).then((value) {
      setState(() {
        isLoadFish = true;
      });
    });
  }

  @override
  void didUpdateWidget(covariant _TabItemPage oldWidget) {
    isLoadFish = false;
    Future.delayed(Duration(seconds: 1)).then((value) {
      setState(() {
        isLoadFish = true;
      });
    });
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: [
        Center(
          child: Text('第${widget.text}个页面${isLoadFish ? '加载完成' : '加载中'}'),
        )
      ],
    );
  }
}
